package com.codefish.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codefish.annotation.PassToken;
import com.codefish.entity.PerInformationEntity;
import com.codefish.service.PerInformationService;
import com.codefish.util.IdWorker;
import com.codefish.util.R;
import com.codefish.util.SmsUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Classname PerInformationController
 * @Description 用户管理
 * @Date 2021/4/29 15:09
 * @Created 学兔斯基眯眼看世界
 */
@RestController
@RequestMapping("/perInformation")
public class PerInformationController {

    @Autowired
    private PerInformationService perInformationService;

    @Autowired
    private IdWorker idWorker;



    /**
     * 条件分页查询
     *
     * @param name
     * @return
     */
    @GetMapping("/getPageByParams")
    @ResponseBody
    public R getPageByParams(@RequestParam(value = "name",required = false)String name
            , @RequestParam(value = "page",required = true)int page
            ,@RequestParam(value = "limit",required = true) int limit) {

        // 条件分页查询
        IPage<PerInformationEntity> entityIPage = perInformationService.getPageByParams(name, page, limit);

        List<PerInformationEntity> reList = entityIPage.getRecords();

        long total = entityIPage.getTotal();

        return R.ok().put("entityList", reList).put("total", total);
    }

    /**
     * 保存
     *
     * @param perInformationEntity
     * @return
     */
    @RequestMapping("/saveOne")
    public R saveOne(@RequestBody PerInformationEntity perInformationEntity) {
        // 根据 telephone 和 username 都要不重复
        if (perInformationService.queryCountByTelephoneAndUsername(perInformationEntity)) {
            return R.error("联系方式和用户名重复");
        }
        perInformationEntity.setId(idWorker.nextId());

        boolean save = perInformationService.save(perInformationEntity);

        if (!save) {
            return R.error("新增失败！");
        }

        return R.ok("新增成功！");
    }

    /**
     * 修改
     */
    @RequestMapping("/updateOne")
    public R updateOne(@RequestBody PerInformationEntity perInformationEntity) {
        // 根据 telephone 和 username 都要不重复
        if (perInformationService.queryCountByTelephoneAndUsername(perInformationEntity)) {
            return R.error("联系方式和用户名重复");
        }
        perInformationEntity.setUpdateTime(new Date());
        perInformationService.updateById(perInformationEntity);
        return R.ok();
    }



    /**
     * 删除单个公共字段
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteOne")
    public R deleteOne(@RequestParam Long id) {
        boolean deleteFlag = perInformationService.deleteOne(id);
        if (!deleteFlag) {
            return R.error("删除失败！");
        }
        return R.ok("删除成功！");
    }

    /**
     * 发送短信验证码
     * @param phone 手机号
     * @return
     */
    @PassToken
    @GetMapping("/sendSms")
    public R sendSms(String phone){
        if (StringUtils.isEmpty(phone)){
            return R.error("手机号不能为空");
        }
        return perInformationService.sendSms(phone);
    }

    /**
     * 用户注册
     * @param perInformationEntity
     * @param smsCode
     * @return
     */
    @PassToken
    @PostMapping("/registrationUser")
    public R registrationUser(@RequestBody PerInformationEntity perInformationEntity,String smsCode){
        return perInformationService.registrationUser(perInformationEntity,smsCode);
    }

}
